<template>
  <div class="right-root">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 2、卡片视图 -->
    <el-card class="box-card">
      <!-- 用户表格 :data绑定userList数据 -->
      <el-table :data="pageInfo.list" border style="width: 100%" :headerCellStyle="headerCellStyle">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" width="100" align="center"> </el-table-column>
        <el-table-column prop="authName" label="权限名称" width="300" align="center"> </el-table-column>
        <el-table-column prop="path" label="路径" width="300" align="center"> </el-table-column>
        <el-table-column prop="level" label="等级" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.level === '0'">一级</el-tag>
            <el-tag v-else-if="scope.row.level === '1'" type="success">二级</el-tag>
            <el-tag v-else-if="scope.row.level === '2'" type="danger">三级</el-tag>
          </template>
        </el-table-column>

        <!-- ！！！！作用域插槽 scope.row是当前行对应的元素对象！！！！ -->
      </el-table>
      <!-- 3、分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="pageInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import request from '../../utils/request.js'
export default {
  data() {
    return {
      rightList: [],
      //表头的样式
      headerCellStyle: {
        backgroundColor: '#409EFF',
        color: '#ffffff',
        textAlign: 'center'
      },
      //2、分页信息
      pageInfo: {
        pagenum: 1,
        //一页几条
        pagesize: 5,
        total: 0,
        list: [],
        start: 0,
        end: 0,
        totalpage: 0
      }
    }
  },
  methods: {
    //一页几条发生变化时，重新加载数据
    handleSizeChange(pagesize) {
      this.pageInfo.pagesize = pagesize
      this.getRightsList()
    },
    //页码发生变化时，重新加载数据
    handleCurrentChange(pagenum) {
      this.pageInfo.pagenum = pagenum
      this.getRightsList()
    },
    //获得权限列表
    async getRightsList() {
      let option = {
        url: 'rights/list',
        type: 'get'
      }
      let { data: res } = await request(option)
      console.log(res)
      //this.pageInfo.list = res.data
      this.rightList = res.data
      //计算总条数
      this.pageInfo.total = this.rightList.length
      //计算总页数
      this.pageInfo.totalpage = Math.ceil(this.pageInfo.total / this.pageInfo.pagesize)
      //计算起点
      let start = (this.pageInfo.pagenum - 1) * this.pageInfo.pagesize
      //计算终点
      let end = start + this.pageInfo.pagesize
      if (end > this.pageInfo.total) {
        end = this.pageInfo.total
      }
      //截取数组元素
      let arr = this.rightList
      this.pageInfo.list = []
      for (let i = start; i < end; i++) {
        this.pageInfo.list.push(arr[i])
      }
    }
  },
  mounted() {
    this.getRightsList()
  }
}
</script>
